package com.inetools.gwetons.client;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArrayString;

public class AbstractNativeJsData extends JavaScriptObject {
	protected AbstractNativeJsData() {
	}
	
	/**
	 * Helper method to return trimmed inputStr since trim method of String class cannot be 
	 * called (like below) from inside of JSNI method:
	 * 	var nameCopy = @java.lang.String::new(Ljava/lang/String;)( name).@java.lang.String::trim()();
	 * This needs to be static method, since JavaScript class cannot be instantiated, 
	 * it seems not to allowed to call this method using "this" instance identifier like: 
	 * 	this.@com.inetools.gwetons.client.DataUnitByStyleSelector::trim(Ljava/lang/String;)( inputStr);
	 */
	public static final String trim( String inputStr) {
		return inputStr.trim();
	}
	
	protected final native String getStringStrictImpl( String name) /*-{
		if ( typeof( name) != "string") return null;
		var nameCopy = @com.inetools.gwetons.client.AbstractNativeJsData::trim(Ljava/lang/String;)( name);
		if ( nameCopy.length < 1) return null;
		if ( typeof( this[ nameCopy]) != "string") return null;
		var propertyStr = @com.inetools.gwetons.client.AbstractNativeJsData::trim(Ljava/lang/String;)( this[ nameCopy]);
		if ( propertyStr.length < 1) return null;
		return propertyStr;
	}-*/;

	protected final native JsArrayString getJsStringArrayImp( String name) /*-{
		if ( typeof( name) != "string") return null;
		var nameCopy = @com.inetools.gwetons.client.AbstractNativeJsData::trim(Ljava/lang/String;)( name);
		if ( nameCopy.length < 1) return null;
		if ( typeof( this[ nameCopy]) != "object") return null;
		var stringArray = this[ nameCopy];
		if ( typeof( stringArray.length) != "number") return null;
		for( var index in stringArray) {
			if ( typeof( stringArray[ index]) != "string") return null;
		} // for
		return stringArray;
	}-*/;

	
	protected final native Boolean getBooleanImpl( String name) /*-{
		if ( typeof( name) != "string") return null;
		var nameCopy = @com.inetools.gwetons.client.AbstractNativeJsData::trim(Ljava/lang/String;)( name);
		if ( typeof( this[ nameCopy]) != 'boolean') return null;
		return @java.lang.Boolean::valueOf(Z)( this[ nameCopy]);
	}-*/;

	protected final native Integer getIntImpl( String name) /*-{
		if ( typeof( name) != "string") return null;
		var nameCopy = @com.inetools.gwetons.client.AbstractNativeJsData::trim(Ljava/lang/String;)( name);
		if ( isNaN( this[ nameCopy])) return null;
		var intValue = parseInt( this[ nameCopy]);
		if ( isNaN( intValue)) return null;
		return @java.lang.Integer::valueOf(I)( intValue);
	}-*/;
}
